package neublaController

import (
	"net/http"
	"tupu-go/controller/nebula/dto"
	"tupu-go/pkg/res"
	"tupu-go/service/buildSchema"

	"github.com/gin-gonic/gin"
)

type SchemaController struct{}

func NewSchemaController() *SchemaController {
	return &SchemaController{}
}

func (s *SchemaController) CreateSchema(c *gin.Context) {
	var schema dto.Schema
	if err := c.ShouldBindJSON(&schema); err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, err)
		return
	}
	// 处理schema
	err := buildSchema.CreateSchema(schema)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}
	res.SuccessResponse(c, "Schema created successfully")
}

// GenerateYAML
// 新增：生成YAML文件接口
func (s *SchemaController) GenerateYAML(c *gin.Context) {
	var schema dto.Schema
	if err := c.ShouldBindJSON(&schema); err != nil {
		res.ErrorResponse(c, http.StatusBadRequest, err)
		return
	}

	// 生成YAML内容
	yamlContent, err := buildSchema.GenerateYAMLContent(schema)
	if err != nil {
		res.ErrorResponse(c, http.StatusInternalServerError, err)
		return
	}

	// 设置响应头为YAML文件
	c.Header("Content-Type", "application/x-yaml")
	c.Header("Content-Disposition", "attachment; filename=importer.yaml")
	c.String(http.StatusOK, yamlContent)
}
